package com.example.wangshuo.zyfypt_205ws.activity;


import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.example.wangshuo.zyfypt_205ws.R;
import com.example.wangshuo.zyfypt_205ws.bean.LoginBean;
import com.example.wangshuo.zyfypt_205ws.bean.SessionidBean;
import com.example.wangshuo.zyfypt_205ws.iface.LoginListener;
import com.example.wangshuo.zyfypt_205ws.model.LoginModel;


public class LoginActivity extends AppCompatActivity implements View.OnClickListener, LoginListener {
    private TextView tv1;
    private TextView tvname;
    private TextView tvpassward;
    private EditText edname;
    private EditText edpassword;
    private Button btland;
    private Button btregister;
    private LoginModel loginModel;
    private SessionidBean sessionIDBean;
    private Switch Switch;
   private String username="";
    private String password="";
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        init();
        btregister.setOnClickListener(this);
        btland.setOnClickListener(this);
        sp=getSharedPreferences("Save",MODE_PRIVATE);

        readSP();
        
    }

    private void readSP() {
        String name=sp.getString("username",null);
        String pass=sp.getString("password",null);
        Boolean b=sp.getBoolean("remember",false);
        if(b){
            edname.setText(name);
            edpassword.setText(pass);
            Switch.setChecked(b);
        }
    }

    private void init() {
        tv1=(TextView)findViewById(R.id.textView);
        tvname=(TextView)findViewById(R.id.textView2);
        tvpassward=(TextView)findViewById(R.id.textView3);
        edname=(EditText)findViewById(R.id.editText);
        edpassword=(EditText)findViewById(R.id.editText2);
        btregister=(Button)findViewById(R.id.button2);
        btland=(Button)findViewById(R.id.button);
        Switch=(Switch)findViewById(R.id.switch1);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button:
                loginModel=new LoginModel();
                loginModel.getResultList(edname.getText().toString(),edpassword.getText().toString(), this);
                username=edname.getText().toString();
                password=edpassword.getText().toString();
                break;
            case R.id.button2:
                startActivity(new Intent(getApplication(),RegisterActivity.class));
                LoginActivity.this.finish();
                break;
        }
    }

    @Override
    public void onResponse(LoginBean beanList) {
        sessionIDBean = new SessionidBean();
        sessionIDBean.getSessionIDBean = beanList.getSessionid();
        String realname=SessionidBean.realName=beanList.getRealname();
        SessionidBean.userName=beanList.getUsername();
        SessionidBean.id= String.valueOf(beanList.getId());
        SessionidBean.sex=String.valueOf(beanList.getSex());
        if(!"".equals(beanList.getError())){
            Toast.makeText(this,"请输入正确的用户名和密码", Toast.LENGTH_SHORT).show();
        }else if(edname.length()==0||  edpassword.length()==0){
            Toast.makeText(this,"请输入用户名和密码", Toast.LENGTH_SHORT).show();
        }else{
            saveSP();

            Toast.makeText(this,"欢迎"+realname+"使用本软件", Toast.LENGTH_SHORT).show();
           // System.out.print(SessionidBean.getSessionIDBean);
            Intent intent=new Intent(LoginActivity.this,MainActivity.class);
            startActivity(intent);


        }
    }

    private void saveSP() {
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("username",username);
        editor.putString("password",password);
        editor.putBoolean("remember",Switch.isChecked());
        editor.commit();
    }

    @Override
    public void onFail(String msg) {

    }
}
